package com.kyle.redpacket.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import reactor.core.publisher.Flux;

/**
 * @author Kyle
 */
@Configuration(
        proxyBeanMethods = false
)
//@ConditionalOnClass({ReactiveRedisConnectionFactory.class, ReactiveRedisTemplate.class, Flux.class})
//@AutoConfigureBefore({RedisReactiveAutoConfiguration.class})
public class RedisReactiveAutoConfigurationX {
    public RedisReactiveAutoConfigurationX() {
    }

    @Bean
//    @ConditionalOnMissingBean(
//            name = {"reactiveRedisTemplate"}
//    )
    @ConditionalOnBean({ReactiveRedisConnectionFactory.class})
    public ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(
            ReactiveRedisConnectionFactory reactiveRedisConnectionFactory,
            ObjectMapper objectMapper) {
//        JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(
//                resourceLoader.getClassLoader());
        GenericJackson2JsonRedisSerializer jdkSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);
        RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext.newSerializationContext().key(
                jdkSerializer).value(jdkSerializer).hashKey(jdkSerializer).hashValue(jdkSerializer).build();
        return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
    }

    /*
    @Bean
    @ConditionalOnMissingBean(
        name = {"reactiveStringRedisTemplate"}
    )
    @ConditionalOnBean({ReactiveRedisConnectionFactory.class})
    public ReactiveStringRedisTemplate reactiveStringRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
        return new ReactiveStringRedisTemplate(reactiveRedisConnectionFactory);
    }
    */

}